﻿package scripts {
	
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	public class zoom extends Sprite {
		private var content1:Sprite;
		private var container:Sprite;
		private var oldX:Number;
		private var oldY:Number;
		private var oldMouseX:Number;
		private var oldMouseY:Number;
		private var dragStart:Boolean;
			
		public function zoom() {
			addlistener(addChild(content1 = createContent()) as Sprite);
		}
		private function addlistener(c:Sprite):void
		{
			c.scrollRect = new Rectangle(0,0,stage.width,stage.height);
			content1.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
			c.scaleX = c.scaleY =1; //2;//
		}
		protected function downHandler(event:MouseEvent):void
		{
			if(dragStart){
				stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			}
			oldX = content1.scrollRect.x;
			oldY = content1.scrollRect.y;
			oldMouseX = event.stageX;
			oldMouseY = event.stageY;
		}
		
		protected function moveHandler(event:MouseEvent):void
		{
			
			const disX:Number = oldMouseX - event.stageX;
			const disY:Number = oldMouseY - event.stageY;
			const rect:Rectangle = content1.scrollRect;
			rect.x = oldX + disX;
			rect.y = oldY + disY;
			content1.scrollRect = rect;
			
		}
		
		protected function upHandler(event:MouseEvent):void
		{
			if((oldMouseX == event.stageX) && (oldMouseY == event.stageY)){
				dragStart = !dragStart;
				content1.scaleX=content1.scaleY=(content1.scaleY==2)?1:2;
				content1.scrollRect = new Rectangle(0,0,stage.width,stage.height);
			}
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		}
		
		
		private function createCanvas():Sprite
		{
			const sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0xFF0000);
			sprite.graphics.drawRect(0,0,stage.width,stage.height);
			sprite.graphics.endFill();
			return sprite;
		}
		private function createContent():Sprite
		{
			const sprite:Sprite = new Sprite();
			sprite.addChild(new jpgPic());
			/*
			sprite.graphics.beginFill(0x000000);
			sprite.graphics.drawRect(0,0,400,300);
			sprite.graphics.endFill();
			*/
			return sprite;
		}
	}
	
}
